package com.greensecurity.sim.controller;

import com.greensecurity.sim.config.RoleNameConfig;
import com.greensecurity.sim.entity.*;
import com.greensecurity.sim.service.GreenService;
import com.greensecurity.sim.service.UserService;
import com.greensecurity.sim.util.ComputeUtil;
import com.greensecurity.sim.util.IsWindowOrLinuxUtils;
import com.greensecurity.sim.util.SplitUser;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/green")
@Slf4j
public class GreenController {

    @Resource
    public GreenService greenService;

    @Resource
    private UserService userService;

//    @ResponseBody
//    @ApiOperation(value = "保存绿地信息")
//    @PostMapping("/save")
//    public String save(@RequestBody Green green) {
//        try {
//            greenService.save(green);
//            return "OK";
//        }catch (Exception e){
//            System.err.println(e.getMessage());
//            return e.getMessage();
//        }
//    }

    @ResponseBody
    @ApiOperation(value = "获取网格编号位数")
    @GetMapping("/getInterNum/{dateTime}")
    public String getInterNum(@PathVariable String dateTime){
        //根据当前日期获取当天最后的一个上传编号
        String last = greenService.getInterNum(dateTime);
        if(last == null){
            return "01";
        }
//        log.info("传入的值:"+dateTime);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMdd");
        try {
            dateTime = f2.format(f.parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String num=last.replace(dateTime,"");
        int numUp = Integer.parseInt(num)+1;
        if (numUp<10){
            num = "0"+numUp;
        }
//        log.info("传入的值:"+dateTime);
        log.info("改变的值:"+num);
//        log.info("数值:"+last.replace(dateTime,""));
        return num;
    }

    @ResponseBody
    @ApiOperation(value = "保存绿地上传信息")
    @PostMapping("/save")
    public User save(@RequestBody User user) {
        SplitUser.newSplitUser(user);
        ComputeUtil.greenCompute(user);
        for (Green green:user.getGreen()){
            try {
                long id = greenService.findByInterNum(green.getInterNum());
                green.setId(id);

            }catch (Exception e){
            }
        }
        userService.save(user);
        return user;
    }
    @ResponseBody
    @ApiOperation(value = "根据角色权限查询全部绿地信息 移动端")
    @PostMapping("/findAllByRole")
    public List<Green> findAll(@RequestBody User user) {
        List<Role> role =user.getRole();
        List<Green> list =new ArrayList<>();
        for(Role role1 :role){
            if (role1.getRoleName().equals(RoleNameConfig.INSERT)){
                list = user.getGreen();
            }else {
                list = greenService.findAll();
                for(Green green : list){
                    for(GreenLandPhoto greenLandPhoto : green.getGreenLandPhotos()){
                        greenLandPhoto.setPhotoUrl(IsWindowOrLinuxUtils.getPhotoUrl()+greenLandPhoto.getPhotoUrl());
                    }
                }
            }

        }
        return list;
    }

    @ResponseBody
    @ApiOperation(value = "随机查询5条绿地信息用于首页显示")
    @GetMapping("/findGreenLandLimit")
    public List<Green> findGreenLandLimit() {
        List<Green> list = greenService.findGreenLimit();
        for(Green green : list){
            for(GreenLandPhoto greenLandPhoto : green.getGreenLandPhotos()){
                greenLandPhoto.setPhotoUrl(IsWindowOrLinuxUtils.getPhotoUrl()+greenLandPhoto.getPhotoUrl());
            }
        }
        return list;
    }

    @ResponseBody
    @ApiOperation(value = "删除绿地信息")
    @GetMapping("/delById/{interNum}")
    public String delByInterNum(@PathVariable String interNum){
        greenService.delByInterNum( greenService.findByInterNum(interNum));
        return "OK";
    }

    @ResponseBody
    @ApiOperation(value = "编辑绿地信息")
    @PostMapping("/update")
    public String update(@RequestBody Green green) {
//        Optional<Green> green1 = greenService.findGreenById(green.getId());
//        Green green2 = green1.get();
//        green.setGreenLandPhotos(green2.getGreenLandPhotos());
//        green.setWaringGrade();
//        greenService.saveGreenLand(greenLand);

        return "保存成功";
    }


    @Test
    public void test(){
        try {

        }catch (Exception e){
            System.out.println(e);
        }
    }
}
